import MNIST.input_data as input_data
import tensorflow as tf
mnist = input_data.read_data_sets('/MNIST/data', one_hot=True)
sess = tf.Session()

# None 表示其值不定
x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])

# 权重
W = tf.Variable(tf.zeros([784,10]))
# 偏置
b = tf.Variable(tf.zeros([10]))
# 回归模型
y = tf.nn.softmax(tf.matmul(x,W) + b)
# 损失函数
cross_entropy = - tf.reduce_sum(y_ * tf.log(y))
# 训练模型
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

# 初始化才能在 session 用
sess.run(tf.global_variables_initializer())

for i in range(1000) :
    batch = mnist.train.next_batch(50)
    train_step.run(feed_dict = {x:batch[0], y_:batch[1]}, session=sess)

# 评估模型
correct_prediction = tf.equal(tf.argmax(y, 1) , tf.argmax(y_, 1))
# 布尔值转换成浮点数
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
# 打印结果
print(accuracy.eval(feed_dict={x: mnist.test.images, y_:mnist.test.labels} , session=sess))


